package com.ysd.resourceusestatistics.service;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import com.ysd.resourceusestatistics.dao.IConsumeLogDao;
import com.ysd.resourceusestatistics.dao.IMemberDao;
import com.ysd.resourceusestatistics.model.ConsumeLog;
import com.ysd.resourceusestatistics.model.Member;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ConsumenLogImpl
 */
@Service
public class ConsumeLogServiceImpl implements IConsumeLogService {

	@Resource
	private IConsumeLogDao consumeLogDao;

	@Resource
	private IMemberDao memberDao;

	@Transactional
	@Override
	public int deleteInBatch(Integer id) {
		return consumeLogDao.deleteInBatch(id);
	}

	@Transactional
	@Override
	public ConsumeLog swipingCard(Integer readRoomId, String cardNo, boolean inOrOut) {
		// 根据卡号获取对象状态
		Member m = memberDao.getCardStatus(cardNo);
		ConsumeLog log = null;
		if (inOrOut) {
			// 刷卡进入
			if (m.getStatus() == 0) {// 状态为正常
				// 进入修改状态信息为1
				m.setStatus(1);
				// 记录刷卡信息
				log = new ConsumeLog();
				log.setCardNo(cardNo);// 记录卡号
				log.setReadRoomId(readRoomId);// 记录资源室id
				// 设置离开时间为+30分刷卡离开则再次更新
				Calendar nowTime = Calendar.getInstance();
				nowTime.add(Calendar.MINUTE, 30);
				log.setOutTime(nowTime.getTime());// 修改离开时间为进入时间+30分
				log = consumeLogDao.save(log);
				// 修改会员卡状态为资源室id
				m.setStatus(readRoomId);
				memberDao.save(m);

			} else {// 状态异常，请联系管理员
				System.out.println("此卡状态异常，请速联系管理员");

			}

		} else {
			// 刷卡离开
			log = consumeLogDao.getLastInTime(cardNo);// 最后一次进入资源室日志编号
			consumeLogDao.updateOutTime(log.getId(), new Date());// 更新离开时间

			m.setStatus(0);
			// 修改会员卡状态为0

		}

		return log;
	}

}